<template>
  <div>
    <!--    <hte-file-class-form ref="realForm" @ok="submitCallback" :disabled="disableSubmit"></hte-file-class-form>-->
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        row-key="id"
        :columns="columns"
        :data-source="tableData"
        :pagination="ipagination"
        :loading="loading"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      />
    </div>
  </div>
</template>

<script>
import { postAction, getAction } from '@/api/manage'

export default {
  name: 'FileRange',
  components: {},
  props: {
    hteFile: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      authorityOptions: [
        { value: '1', label: '查阅' },
        { value: '2', label: '编辑' },
        { value: '3', label: '下载' },
        { value: '4', label: '可废除' },
      ],
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '对象类型 ',
          align: 'center',
          dataIndex: 'objectType',
          customRender: (text) => {
            if (text == 1) {
              return '私有组'
            } else if (text == 2) {
              return '发送人员'
            }else if (text == 3) {
              return '组织架构';
            }
            else if (text == 4) {
              return '所有人公开';
            }
            return text
          },
        },

        {
          title: '对象名称',
          align: 'center',
          dataIndex: 'objectName',
        },
        {
          title: '权限',
          align: 'center',
          dataIndex: 'authority',
          customRender: (text) => {
            var str = ''
            if (text && text.indexOf('1') >= 0) {
              str += '查阅,'
            }
            if (text && text.indexOf('2') >= 0) {
              str += '编辑,'
            }
            if (text && text.indexOf('3') >= 0) {
              str += '下载,'
            }
            if (text && text.indexOf('4') >= 0) {
              str += '可废除,'
            }
            if (str.length > 1) {
              return str.substr(0, str.length - 1)
            }
            return str
          },
        },
        {
          title: '创建人',
          align: 'center',
          dataIndex: 'createUserName',
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: '客户端',
          align: 'center',
          dataIndex: 'client',
        },
      ],
      tableData: [],
      dataSource: [],
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
        name: '',
      },
      loading: false,
      title: '',
      width: 800,

      disableSubmit: false,
      data: [],
      selectedRowKeys: [],
      rowKeyIndex: -1,
      addVisible: false,
      confirmLoading: false,
      model: { objectType: 1, client: '海芯', objectId: [] },
      validatorRules: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      objData: [],
      fetching: false,
      url: {
        save: '/lib/hteLibFileRange/add',
        list: '/lib/hteLibFileRange/list',
        deleteBatch: '/lib/hteLibFileRange/deleteBatch',
        getUserList: '/lib/hteFileClass/getUserList',
        getGroupList: '/lib/hteLibraryGroup/groupList',
      },
    }
  },

  created() {
    this.loadRange()
  },
  methods: {
    loadRange() {
      let param = Object.assign({}, this.ipagination)
      param.classId = this.hteFile.fileType
      delete param.pageSizeOptions
      delete param.showTotal
      getAction(this.url.list + '/' + this.hteFile.id, param).then((res) => {
        if (res.success) {
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          this.tableData = res.result.records
        }
      })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleTableChange(pagination, filters, sorter) {
      // 分页、排序、筛选变化时触发
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = sorter.order === 'ascend' ? 'asc' : 'desc'
      }
      this.ipagination = pagination
      this.ipagination.pageNo = this.ipagination.current
      this.loadRange()
    },
  },
  watch: {
    'hteFile.id': {
      handler: function (newVal, oldVal) {
        if (newVal != oldVal) {
          this.loadRange()
        }
      },
    },
  },
}
</script>
